home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 March / Macworld (1998-03) (Disk 1).dmg / Shareware World / Utilities / Text Processing / Alpha / Tcl / Packages / compare.tcl next >
Encoding:
Text File  |  1997-12-10  |  3.9 KB  |  140 lines  |  [TEXT/ALFA]

  1. #========================(install)==========================================
  2. # Compare Windows.
  3. # Simplified (and improved) version of David C. Black's 'compare-windows'.
  4. # Modified by Mark Nagata, 2/23/93, corrected, 2/24/93.
  5. # Sped-up version, 2/25/93.
  6. #
  7. # The return position bug in David's routine (when $patt != "") 
  8. # is fixed in this version.
  9. # Vince renamed a couple of things and added the 'package' stuff so
  10. # this works smoothly with the new Alpha Tcl scheme.  The bindings
  11. # can now be adjusted via a preferences dialog.  Also rewrote a few
  12. # bits to try to avoid window-toggling.
  13. #===========================================================================
  14.  
  15. alpha::extension compareWindows 0.23 {
  16.     namespace eval compare {}
  17.     menu::insert Utils submenu 0 compare
  18.     menu::insert "compare" items end windowsInPlace
  19.     hook::register requireOpenWindowsHook [list compare windowsInPlace] 2
  20.     newPref binding findDifference "/`«X»" compareWindows "" compare::windowsInPlace
  21.     newPref binding findDifferenceIgnoringSpace "/1«X»" compareWindows "" compareOpt
  22.     newPref binding findNextDifference "<U/`«X»" compareWindows "" compareNext
  23.     newPref binding findNextDifferenceIgnoringSpace "<U/1«X»" compareWindows "" compareOptNext
  24.     package::addPrefsDialog compareWindows
  25. }
  26. ####
  27. # On my Extended Keyboard (where the backquote key is to the left of the 
  28. # "1" key), I bind prefix-(shift)-backquote to 'compare(Next)' and
  29. # prefix-(shift)-1 to 'compareOpt(Next)', as in the above.
  30. # On my Powerbook keyboard (where nothing is to the left of the "1" key),
  31. # I bind prefix-(shift)-1 to 'compare(Next)' and
  32. # prefix-(shift)-2 to 'compareOpt(Next)', respectively.
  33. ####
  34.  
  35. proc compareOpt {} {
  36.     compare::windowsInPlace {-w}
  37. }
  38.  
  39. proc compare::windowsInPlace {args} {
  40.     set    patt {}
  41.     if {$args == "-w"} {
  42.         set patt "\[ \t\n\r\]+"
  43.     }
  44.  
  45.     set    files [winNames -f]
  46.     if {[llength $files] < 2} {
  47.         alertnote "If you want to Compare texts, you need two windows."
  48.         return
  49.     }
  50.  
  51.     watchCursor
  52.     for {set i 1} {$i < 3} {incr i} {
  53.         set    wn($i) [lindex $files [expr $i -1]]
  54.         set    wp($i) [getPos -w $wn($i)]
  55.         select -w $wn($i) $wp($i) $wp($i)
  56.         set    wrt($i) [getText -w $wn($i) $wp($i) [maxPos -w $wn($i)]]
  57.         set wt($i) $wrt($i)
  58.         if {$patt != ""} {
  59.             regsub -all    $patt $wt($i) " " wt($i)
  60.         }
  61.     }
  62.         
  63.     # Exactly equal
  64.     if {$wt(1) == $wt(2)} {
  65.         alertnote    "The windows match from cursors to ends."
  66.         return
  67.     }
  68.     
  69.     # Only consider    smaller    of two strings
  70.     set    siz    [string    length $wt(1)]
  71.     if {$siz > [string length $wt(2)]} {
  72.         set    siz    [string    length $wt(2)]
  73.     }
  74.     
  75.     # Equal    except for added stuff
  76.     set    l [expr    $siz-1]
  77.     if {[string    range $wt(1) 0 $l] == [string range    $wt(2) 0 $l]} {
  78.         set    beg    $siz
  79.         set    offset(1) $beg
  80.         set    offset(2) $beg
  81.     } else {
  82.         set    beg    0
  83.         
  84.         while {$siz} {
  85.             set    siz    [expr $siz/ 2]
  86.             set    end    [expr $beg+$siz]
  87.             if {[string    range $wt(1) $beg $end]    == [string range $wt(2)    $beg $end]}    {
  88.                 incr beg $siz
  89.                 incr beg
  90.             }
  91.         }
  92.         set    offset(1) $beg
  93.         set    offset(2) $beg
  94.     }
  95.     for {set i 2} {$i > 0} {incr i -1} {
  96.         set count $offset($i)
  97.         set pos [expr $wp($i)+$count]
  98.         if {$patt != ""} {
  99.             set ans [string    range $wt($i) 0 [expr $offset($i)-1]]
  100.             set lans [string length $ans]
  101.             set tt [string range $wrt($i) 0 [expr $count-1]]
  102.             regsub -all $patt $tt " " tt
  103.             set ltt [string length $tt]
  104.             while {$ltt < $lans} {
  105.                 incr count [expr $lans-$ltt]
  106.                 incr pos [expr $lans-$ltt]
  107.                 message $pos
  108.                 set tt [string range $wrt($i) 0 [expr $count-1]]
  109.                 regsub -all $patt $tt " " tt
  110.                 set ltt [string length $tt]
  111.             }
  112.         }
  113.         
  114.         set pos [expr $pos > [maxPos -w $wn($i)] ? [maxPos -w $wn($i)] : $pos]
  115.         display -w $wn($i) [expr $pos -1]
  116.         select -w $wn($i) $pos [incr pos]
  117.         refresh $wn($i)
  118.     }
  119.     message "difference found"
  120.     return
  121. }
  122.  
  123. proc compareNext {} {
  124.     endOfLine
  125.     catch {bringToFront [lindex [winNames -f] 1]}
  126.     endOfLine
  127.     compare::windowsInPlace
  128. }
  129.  
  130. proc compareOptNext {} {
  131.     endOfLine
  132.     catch {bringToFront [lindex [winNames -f] 1]}
  133.     endOfLine
  134.     compareOpt
  135. }
  136.  
  137.